
const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
const user = require('../../../utils/user.js');

//index.js
//获取应用实例
const app = getApp()
// pages/ucenter/listorder/listorder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    order:{},
    goodsid:'',
    ll:{},
    handleOption0: false,
    handleOption1:false,
    handleOption2: false,
    handleOption3: false,
    handleOption4: false,
    handleOption5: false,
    handleOption6: false,
    handleOption7: false,
    goods:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      id:options.orderSn
    })
    this.getdetail()
  },
  getdetail() {
    let that = this
    util.request(api.QueryOrderInfo+this.data.id, {
      
    }, 'post', {
        'Content-Type': 'application/json;charset=UTF-8',
        'X-Qxmall-Token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log(res)
       if(res.code==200){
         that.setData({
           order:res.data.orderInfo,
           goodsid: res.data.orderInfo.commodityId
         })
         that.getdetaill()
         if (res.data.orderInfo.status==0){
           that.setData({
             handleOption0: true
           })
         }
         if (res.data.orderInfo.status == 1) {
           that.setData({
             handleOption1: true
           })
         }
         if (res.data.orderInfo.status == 2) {
           that.setData({
             handleOption2: true
           })
         }
         if (res.data.orderInfo.status == 3) {
           that.setData({
             handleOption3: true
           })
         }
         if (res.data.orderInfo.status == 4) {
           that.setData({
             handleOption4: true
           })
         }
         if (res.data.orderInfo.status == 5) {
           that.setData({
             handleOption5: true
           })
         }
         if (res.data.orderInfo.status == 6) {
           that.setData({
             handleOption6: true
           })
         }
         if (res.data.orderInfo.status == 7) {
           that.setData({
             handleOption7: true
           })
         }
       }
      });
  },

  getdetaill() {
    let that = this
    util.request(api.GoodsDetail, {
      id: that.data.goodsid
    }, 'post', {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'X-Qxmall-Token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log(res)
        that.setData({
          goods: res.data.commodity
        })


      });
  },

  //去付款
  payOrder(){
    util.request(api.ToBePaidPay + 'orderSn=' + this.data.order.orderSn + '&id=' + this.data.id, {

    }, 'post', {
        'Content-Type': 'application/json;charset=UTF-8',
        'X-Qxmall-Token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log(res)
        if (res.code === 200) {
          const orderSn = res.data.id
          const bb = res.data.orderSn
          const payParam = res.data.payment;
          console.log("支付过程开始");
          wx.requestPayment({
            'timeStamp': payParam.timeStamp,
            'nonceStr': payParam.nonceStr,
            'package': payParam.packageValue,
            'signType': payParam.signType,
            'paySign': payParam.paySign,
            'success': function (res) {
              console.log("支付过程成功");
              wx.redirectTo({
                url: '/pages/ucenter/payresult/payresult?status=1&orderSn=' + orderSn + '&bb=' + bb
              });
            },
            'fail': function (res) {
              console.log("支付过程失败");
              wx.redirectTo({
                url: '/pages/ucenter/payresult/payresult?status=0&orderSn=' + orderSn + '&bb=' + bb
              });
            },
            'complete': function (res) {
              console.log("支付过程结束")
            }
          });
        } else {
          // wx.redirectTo({
          //   url: '/pages/ucenter/payresult/payresult?status=0&orderSn=' + orderSn
          // });
        }
      }
      )

  },
  // 取消订单
  cancelOrder() {
    util.request(api.CancelOrder + this.data.id, {

    }, 'post', {
        'Content-Type': 'application/json;charset=UTF-8',
        'X-Qxmall-Token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log(res)
        if (res.code == 200) {
          wx.navigateTo({
            url: '../list/list?id=0',
          })
        }
      }
      )
  },
  //申请退款
  refundOrder() {
    let that=this
    util.request(api.ApplyRefund + this.data.id, {

    }, 'post', {
        'Content-Type': 'application/json;charset=UTF-8',
        'X-Qxmall-Token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log(res)
        if(res.code==200){
          wx.navigateTo({
            url: '../list/list?id=0',
          })
        }
      }
      )
  },
  //确认收货
  confirmOrder() {
    util.request(api.ConfirmReGoods + this.data.id, {

    }, 'post', {
        'Content-Type': 'application/json;charset=UTF-8',
        'X-Qxmall-Token': wx.getStorageSync('token')
      }).then(function (res) {
        console.log(res)
        if(res.code==200){
          wx.navigateTo({
            url: '../list/list?id=2',
          })
        }
      }
      )
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})